package com.py.sale.service.impl;

import java.util.Collections;
import java.util.List;

import com.py.sale.domain.PyAttachment;
import com.py.sale.domain.SysUser;
import com.py.sale.mapper.PyAttachmentMapper;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;

import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.py.sale.domain.OdSaleDetails;
import com.py.sale.mapper.OdSaleMapper;
import com.py.sale.domain.OdSale;
import com.py.sale.service.IOdSaleService;

import javax.annotation.Resource;

/**
 * 销售Service业务层处理
 * 
 * @author 童佳
 * @date 2024-12-03
 */
@Service
public class OdSaleServiceImpl implements IOdSaleService 
{
    @Resource
    private OdSaleMapper odSaleMapper;

    @Resource
    private PyAttachmentMapper pyAttachmentMapper;

    /**
     * 查询销售
     * 
     * @param saleId 销售主键
     * @return 销售
     */
    @Override
    public OdSale selectOdSaleBySaleId(Integer saleId)
    {
        OdSale odSale=odSaleMapper.selectOdSaleBySaleId(saleId);
        OdSaleDetails odSaleDetails=new OdSaleDetails();
        odSaleDetails.setSaleDetailsCode(odSale.getSaleCode());
        System.out.println(odSaleDetails);
        List<OdSaleDetails> list=odSaleMapper.selectOdSaleDetailsList(odSaleDetails);
        System.out.println(list.toString());
        odSale.setOdSaleDetailsList(list);
        PyAttachment pyAttachment=new PyAttachment();
        pyAttachment.setOwnershipNumber(odSale.getSaleCode());
        odSale.setFileList(Collections.singletonList(pyAttachmentMapper.selectPyAttachmentList(pyAttachment)));
        return odSale;
    }

    /**
     * 查询销售列表
     * 
     * @param odSale 销售
     * @return 销售
     */
    @Override
    public List<OdSale> selectOdSaleList(OdSale odSale)
    {
        return odSaleMapper.selectOdSaleList(odSale);
    }

    /**
     * 新增销售
     * 
     * @param odSale 销售
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOdSale(OdSale odSale)
    {
        odSale.setIsDel("0");
        int rows = odSaleMapper.insertOdSale(odSale);
        insertOdSaleDetails(odSale);
        return rows;
    }

    /**
     * 修改销售
     * 
     * @param odSale 销售
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOdSale(OdSale odSale)
    {
        System.out.println("修改："+odSale);
        List<OdSaleDetails> list=odSale.getOdSaleDetailsList();
        list.forEach(l->{
            odSaleMapper.deleteDetailsByPhCode(l.getSaleDetailsCode());
        });
        insertOdSaleDetails(odSale);
        return odSaleMapper.updateOdSale(odSale);
    }

    /**
     * 批量删除销售
     * 
     * @param saleIds 需要删除的销售主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOdSaleBySaleIds(Integer[] saleIds)
    {
        String[] values = new String[saleIds.length];
        int index = 0;
        for (Integer saleId : saleIds) {
            OdSale order = odSaleMapper.selectOdSaleBySaleId(saleId);
            // 假设你想将每个订单的某个属性（例如 phCode）添加到 values 数组中
            values[index++] = order.getSaleCode();
        }
        odSaleMapper.deleteOdSaleDetailsBySaleDetailsCodes(values);
        return odSaleMapper.deleteOdSaleBySaleIds(saleIds);
    }

    /**
     * 删除销售信息
     * 
     * @param saleId 销售主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOdSaleBySaleId(Integer saleId)
    {
        OdSale odSale=odSaleMapper.selectOdSaleBySaleId(saleId);
        odSaleMapper.deleteOdSaleDetailsBySaleDetailsCode(odSale.getSaleCode());
        return odSaleMapper.deleteOdSaleBySaleId(saleId);
    }

    @Override
    public SysUser selectBySysUserName(String userName) {
        return odSaleMapper.selectBySysUserName(userName);
    }

    @Override
    public int checkById(Map<String, Object> map) {
        return odSaleMapper.checkById(map);
    }

    @Override
    public int revokeById(Integer sale_id) {
        return odSaleMapper.revokeById(sale_id);
    }

    /**
     * 新增${subTable.functionName}信息
     * 
     * @param odSale 销售对象
     */
    public void insertOdSaleDetails(OdSale odSale)
    {
        List<OdSaleDetails> odSaleDetailsList = odSale.getOdSaleDetailsList();
        String saleCode = odSale.getSaleCode();
        if (StringUtils.isNotNull(odSaleDetailsList))
        {
            List<OdSaleDetails> list = new ArrayList<OdSaleDetails>();
            for (OdSaleDetails odSaleDetails : odSaleDetailsList)
            {
                odSaleDetails.setSaleDetailsCode(saleCode);
                odSaleDetails.setAlreadyOutNum(0L);
                odSaleDetails.setRejectedNum(0L);
                odSaleDetails.setIsDel("0");
                list.add(odSaleDetails);
            }
            if (list.size() > 0)
            {
                odSaleMapper.batchOdSaleDetails(list);
            }
        }
    }

    @Override
    public int colseById(Integer saleId) {
        return odSaleMapper.colseById(saleId);
    }

    @Override
    public List<OdSale> selectOdSaleOrderByInRejected(OdSale odSale) {
        return odSaleMapper.selectOdSaleOrderByInRejected(odSale);
    }

    @Override
    public Map<String, Object> selectBySaleCode(String saleCode) {
        return odSaleMapper.selectBySaleCode(saleCode);
    }
}
